package com.example.fw;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactObject;
import com.example.tests.GroupObject;

public class ContactHelper extends HelperWithWebDriverBase{

	private Set<ContactObject> contactCache;
	
	private void buildContactCache() {
		 contactCache = new HashSet<ContactObject>();
		manager.getNavigationHelper().openMainPage();
		List<WebElement> checkboxes = findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			title=title.substring("Select (".length(), title.length()-")".length());
			//String[] splitted =title.split(" ");
			ContactObject contact = new ContactObject()
					.setTitle(title)
					.setId(checkbox.getAttribute("value"));
			contactCache.add(contact);
		}
		//return contacts;
	}
	
	public Set<ContactObject> getGroups() {
		if (contactCache == null) {
			buildContactCache();
		}
		return contactCache;
	}
 
	public ContactHelper (ApplicationManager manager) {
		super(manager);	
	}
	
	public Set<ContactObject> getContacts() {
		manager.getNavigationHelper().openMainPage();
		List<WebElement> checkboxes = findElements(By.name("selected[]"));
		Set<ContactObject> contacts = new HashSet<ContactObject>();
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			title=title.substring("Select (".length(), title.length()-")".length());
			//String[] splitted =title.split(" ");
			ContactObject contact = new ContactObject()
					.setTitle(title)
					.setId(checkbox.getAttribute("value"));
			contacts.add(contact);
		}
		return contacts;
	}

	private void returnToHomePage() {
		click(By.linkText("home"));
	}
	
	private void submitAddressForm() {
		click(By.name("submit"));
	}

	private void fillContactForm(ContactObject contactObject) {
		typeInContactForm(contactObject);
		selectFromList(By.name("bday"), contactObject.bday);
		selectFromList(By.name("bmonth"), contactObject.bmonth);
		selectFromList(By.name("new_group"), contactObject.newgroup);
	}

	private void typeInContactForm(ContactObject contactObject) {
		type(By.name("firstname"), contactObject.firstname);
		type(By.name("lastname"), contactObject.lastname);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.home);
		type(By.name("mobile"), contactObject.mobile);
		type(By.name("work"), contactObject.work);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.phone2);
		type(By.name("byear"), contactObject.byear);
	}
	
	private void fillContactModificationForm(ContactObject contactObject) {
		typeInContactForm(contactObject);
		selectFromList(By.name("bday"), contactObject.bday);
		selectFromList(By.name("bmonth"), contactObject.bmonth);	
	}
	
	public void createContact(ContactObject contactObject) {
		manager.getNavigationHelper().goToAddNewPage();
		fillContactForm(contactObject);
		submitAddressForm();
		returnToHomePage();
		buildContactCache();
	}
	
	public void modifyContact(int i, ContactObject contactObject) {
		returnToHomePage();
		initContactModification(i);
		fillContactModificationForm(contactObject);
		submitContactModificationForm();
		returnToHomePage();
		buildContactCache();
	}

	private void submitContactModificationForm() {
		click(By.name("update"));
	}

	private void initContactModification(int i) {
		click(By.xpath("//td[7]/a/img"));
	}
	
	public void deleteContact(int i) {
		returnToHomePage();
		initContactModification(i);
		submitContactRemoval();
		returnToHomePage();
		buildContactCache();
	}
	
	private void submitContactRemoval() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}


}
